package com.seadragon.apps.doctorcard.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.seadragon.apps.doctorcard.model.Contact;
import com.seadragon.apps.doctorcard.model.Request;
import com.seadragon.apps.doctorcard.model.RequestStatus;

public interface RequestRepository extends JpaRepository<Request, Long> {
	@Query("select r from Request r where r.sender=:sender and r.status=:status")
	List<Request> findAllRequestsSent(@Param("sender") Contact sender);
	
	@Query("select r from Request r where r.sender=:contact or r.recipient=:contact")
	List<Request> findAllRequests(@Param("contact") Contact contact);
	
	@Query("select r from Request r where (r.sender=:contact or r.recipient=:contact) and r.status=:status")
	List<Request> findAllRequestsByStatus(@Param("contact") Contact contact, @Param("status") RequestStatus status);
	
	@Query("select r from Request r where r.recipient=:recipient")
	List<Request> findAllRequestsReceived(@Param("recipient") Contact recipient);
	
	@Query("select r from Request r where r.recipient=:recipient and r.status=:status")
	List<Request> findAllRequestsReceivedByStatus(@Param("recipient") Contact recipient, @Param("status") RequestStatus status);
	
	@Query("select r from Request r where (r.sender=:sender and r.recipient=:recipient) or (r.sender=:recipient and r.recipient=:sender)")
	Request findBySenderAndRecipient(@Param("sender") Contact sender, @Param("recipient") Contact recipient);
	
	int countByRecipientAndStatus(Contact recipient, RequestStatus status);
}